package com.suitsoft.ut.web.middle.dabian;

import java.io.File;
import java.util.List;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.google.common.collect.Lists;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.commons.UploadFileInfo;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.ServiceException;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.commons.UploadFileManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.util.upload.UploadFileUtil;
import com.suitsoft.ut.util.upload.UploaderDBUtil;
import com.suitsoft.ut.util.upload.XdbFiles;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 上传答辩PPT
 *@author Suineg
 */
@Results({@Result(name=CrudActionSupport.RELOAD,location="upload-pptt",type="redirect")})
public class UploadPpttAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5422928322446972987L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 学生管理  服务层对象  */
	private StudentManager studentManager;
	/** 流程管理 服务层对象 */
	private ThesisFileManager fileManager;
	/***/
	private UploadFileManager uploadFileManager;
	
	
	/** 学生实体 */
	private Student student;
	/** 上传文件 */
	private File uploadFile;
	/** 上传文件名 */
	private String uploadFileFileName;
	/** 上传文件列表 */
	private List<XdbFiles> xdbFiles = Lists.newArrayList();
	/** 文件ID */
	private Long fid;
	/** 已上传文件集合 */
	private List<UploadFileInfo> uploadList = Lists.newArrayList();
	
	@Override
	public String execute() throws Exception {
		student = studentManager.findByUser(UserHolder.getLoginUser().getId());
		if(student == null){
			addActionMessage("请选择使用学生账号");
			return SUCCESS;
		}
		thesis = student.getThesis();
		if(thesis == null){
			addActionMessage("数据有误 。学生不应该没有题目!");
			return 	SUCCESS;
		}
		thesisFile = thesis.getThesisFile();
		if(thesisFile.getIsSelect() ==null ||thesisFile.getIsSelect()  != 1L){
			addActionMessage("你没被选中！");
			return SUCCESS;
		}
//		UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
//		xdbFiles = dbUtil.getFilesByUid(thesis.getId().toString(),UploaderDBUtil.MIDDLE);
		uploadList = thesis.getMiddleFile();
		return SUCCESS;
	}

	/**
	 * 上传PPT
	 * @return
	 */
	@LogAnnotation(log = "上传答辩PPT - 上传")
	public String upload() throws Exception{
		try {
			
			if(id == null || id == 0L){
				throw new ServiceException("非法操作");
			}
		//	UploadFileUtil.upload(uploadFile, id, uploadFileFileName,UploaderDBUtil.DABIAN, ServletActionContext.getServletContext());
			thesis = thesisManager.get(id);
			uploadFileManager.upload(uploadFile, thesis, uploadFileFileName, UploaderDBUtil.MIDDLE, ServletActionContext.getServletContext());
			thesisFile = thesis.getThesisFile();
			thesisFile.setIsUploadPpt(1L);
			fileManager.save(thesisFile);
		} catch (Exception e) {
			e.printStackTrace();
			Struts2Utils.getResponse().sendError(502, "有出错啦");
		}
		return null;
	}
	@LogAnnotation(log = "上传答辩PPT - 删除")
	public String delete(){
		if(id == null || id == 0L || fid == null || fid == 0L){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
	//		UploaderDBUtil dbUtil = new UploaderDBUtil(ServletActionContext.getServletContext());
	//		dbUtil.Delete(id.toString(), fid.toString());
//			xdbFiles = dbUtil.getFilesByUid(id.toString(),UploaderDBUtil.MIDDLE);
//			if(xdbFiles == null || xdbFiles.isEmpty()){
//				thesisFile = thesisManager.get(id).getThesisFile();
//				thesisFile.setIsUploadPpt(null);
//				fileManager.save(thesisFile);
//			}
			
			uploadFileManager.delete(fid);
			
			thesis = thesisManager.get(id);
			uploadList = thesis.getDaBianFile();
			if(uploadList == null || uploadList.isEmpty()){
				thesisFile = thesis.getThesisFile();
				thesisFile.setIsUploadPpt(null);
				fileManager.save(thesisFile);
			}
			
			addActionMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
		}
		
		return RELOAD;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public StudentManager getStudentManager() {
		return studentManager;
	}
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public List<XdbFiles> getXdbFiles() {
		return xdbFiles;
	}
	public void setXdbFiles(List<XdbFiles> xdbFiles) {
		this.xdbFiles = xdbFiles;
	}
	public Long getFid() {
		return fid;
	}

	public void setFid(Long fid) {
		this.fid = fid;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}

	public List<UploadFileInfo> getUploadList() {
		return uploadList;
	}

	public void setUploadList(List<UploadFileInfo> uploadList) {
		this.uploadList = uploadList;
	}
	@Autowired
	public void setUploadFileManager(UploadFileManager uploadFileManager) {
		this.uploadFileManager = uploadFileManager;
	}
	
	
}

